"""
Case Type   : mot
Case Name   : 打开enable_mot_server参数，创建普通表及mot表，连表查询
Create At   : 2025/4/1
Owner       : @li-xin12345
Description : (编译带mot，不编译ENABLE_X86_RDTSCP，对应转测包)
    1、设置enable_incremental_checkpoint=off，enable_mot_server=on
    2、创建普通表及mot表
    3、连表查询
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class MotTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.param_names = ['enable_incremental_checkpoint',
                            'enable_mot_server']
        self.default_values = [self.common.show_param(i) for i in
                               self.param_names]
        self.tb_name = 't_guc_mot_0020'

    def test_mot(self):
        text = '-----step1:设置enable_incremental_checkpoint=off，' \
               'enable_mot_server=on;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc('set',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.param_names[0]}=off')
        self.log.info(res)
        self.assertTrue(res, "执行gs_guc set失败，预期成功但未找到标志信息")
        res = self.pri_sh.execute_gsguc('set',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.param_names[1]}=on')
        self.log.info(res)
        self.assertTrue(res, "执行gs_guc set失败，预期成功但未找到标志信息")
        res = self.pri_sh.restart_db_cluster()
        self.log.info(res)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status)
        res = [self.common.show_param(i) for i in self.param_names]
        self.log.info(res)
        self.assertEqual(['off', 'on'], res, '执行失败：' + text)

        text = '-----step2:创建普通表及mot表;expect:成功-----'
        self.log.info(text)
        sql = f'''drop table if exists {self.tb_name}_01 cascade;
        create table {self.tb_name}_01 (
            id int primary key,
            name varchar(50) not null,
            age int
        );
        drop foreign table if exists {self.tb_name}_02 cascade;
        create foreign table {self.tb_name}_02 (
            id int primary key,
            name varchar(50) not null,
            age int
        );
        insert into {self.tb_name}_01 (id, name, age) values (1, 'Alice', 25),
         (2, 'Bob', 30);
        insert into {self.tb_name}_02 (id, name, age) values (1, 'Charlie', 35),
         (2, 'David', 40);
        '''
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, res,
                      '执行失败：' + text)
        self.assertEqual(2, res.count(self.constant.INSERT_SUCCESS_MSG),
                         '执行失败：' + text)

        text = '-----step3:连表查询;expect:成功-----'
        self.log.info(text)
        sql = f'''select a.id, a.name, b.age from {self.tb_name}_01 a 
        join {self.tb_name}_02 b on a.id = b.id;
        '''
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn('2 rows', res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step4:清理环境; expect:成功-----'
        self.log.info(text)
        sql = f'''drop table if exists {self.tb_name}_01 cascade;
        drop foreign table if exists {self.tb_name}_02 cascade;
        checkpoint;
        '''
        drop_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(drop_res)
        res = [self.common.show_param(i) for i in self.param_names]
        self.log.info(res)
        if res != self.default_values:
            for i, param_name in enumerate(self.param_names):
                res = self.pri_sh.execute_gsguc(
                    'set', self.constant.GSGUC_SUCCESS_MSG,
                    f'{param_name}={self.default_values[i]}')
                self.log.info(res)
            restart_res = self.pri_sh.restart_db_cluster()
            self.log.info(restart_res)
            status = self.pri_sh.get_db_cluster_status()
            self.log.info(status)
            show_param = [self.common.show_param(i) for i in self.param_names]
            self.log.info(show_param)
            self.assertEqual(self.default_values, show_param, '执行失败：' + text)
            self.assertTrue(restart_res, '执行失败：' + text)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, drop_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_res,
                      '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')